#coding:utf-8

from bmob import *
import json
import time
import datetime
import pdb

class TodayTask:
    def __init__(self, table_name):
        self.b = Bmob("f5b3dbd027720a6f49c87b6a1a1c7a84", "616ee9149ae817bcd2f535b9a21050d4")

    def getPlans(self):
        ret_str = self.b.find(
            "t_plan",
            BmobQuerier().addWhereNotEqualTo("user_id","")
            ).stringData
        lst_ret = json.loads(ret_str)['results']
        return lst_ret

    def getTasks(self, plan_list):
        today = datetime.date.today()
        #pdb.set_trace()
        #print(str(today))
        task_list = []
        for item in plan_list:
            print(item['plan_name'] + ":" + item['objectId'])
            ret_str = self.b.find(
                "t_plan_item",
                #BmobQuerier().addWhereEqualTo("plan_id", item['objectId']),
                #BmobQuerier().addWhereEqualTo("item_time", str(today))
                where={"plan_id":item['objectId'],"item_time":str(today)}
            ).stringData
            #pdb.set_trace()
            if ret_str == "":
                continue
            lst_ret = json.loads(ret_str)['results']
            if len(lst_ret) == 0:
                continue
            dct_record = dict()
            dct_record['plan_id'] = item['objectId']
            dct_record['plan_name'] = item['plan_name']
            dct_record['target_usr'] = item['user_id']
            lst_tasks = []
            #pdb.set_trace()
            for i in lst_ret:
                lst_tasks.append(i['item_title'])
            dct_record['tasks'] = lst_tasks
            task_list.append(dct_record)
        return task_list

    def genService(self,target_user, plan_name,plan_id, plan_item):
        url = "htservice://plan_exec/" + plan_id
        print(
            self.b.insert(
		    't_service', # 表名
		    BmobUpdater.increment(
			    "count", # 原子计数key
			    0, # 原子计数值
			    { # 额外信息
				    "target_user_id": target_user,
                    "url" : url,
                    "service_type": 1,
                    "title": plan_name,
                    "comment":plan_item
			    }
		    )
	    ).jsonData # 输出json格式的内容
)
        
def query_test():
    bomb = Bmob("f5b3dbd027720a6f49c87b6a1a1c7a84", "616ee9149ae817bcd2f535b9a21050d4")
    ret_str = bomb.find( # 查找数据库
		"t_plan_item", # 表名
		where={"plan_id":"8f178f1f4c","item_time":"2019-07-03"}
	).stringData # 输出string格式的内容
    lst_ret = json.loads(ret_str)['results']
    print(lst_ret)


if __name__ == "__main__":
    today_task = TodayTask('t_plan')
    print("##########开始查询t_plan表##########")
    plans = today_task.getPlans()
    #for it in plans:
    #    print(it['objectId'] + ":" + it['plan_name'])
    print("##########开始查询t_plan_item表##########")
    tasks = today_task.getTasks(plans)
    #print(tasks)
    print("##########开始写入t_service表##########")
    for it in tasks:
        #pdb.set_trace()
        target_user_id = it['target_usr']['objectId']
        plan_name = it['plan_name']
        plan_id = it['plan_id']
        str_splite = ','
        comment = str_splite.join(it['tasks'])
        today_task.genService(target_user_id, plan_name, plan_id, comment)

